package cn.cs.controller;

import cn.cs.common.*;
import cn.cs.pojo.AdBanner;
import cn.cs.service.AdBannerService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/adBanner")
public class AdBannerController {

    @Resource
    private AdBannerService service;


    /****
     * 获取广告banner接口
     * @param request
     * @param
     * @return
     */
    @RequestMapping("/getAllAdBanners")
    @ResponseBody
    public Dto getAllAdBanners(HttpServletRequest request, HttpServletResponse response){
        Dto dto=null;
        try {
            response.setHeader("Access-Control-Allow-Origin", "*");
            dto=service.getAllAdBanners(request);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("添加失败","0002");
        }
        return dto;
    }
    /**
     * 分页查询 广告banner接口
     *
     * @param pageNumber
     * @param pageSize
     * @return
     * @throws Exception TODO 添加搜索
     */
    @RequestMapping(value = "/csPlatform/getAdBannerList")
    public Dto getAdBannerList(Integer pageNumber, Integer pageSize) throws Exception {
        Map<String, Object> param = new HashMap<String, Object>();
        return DtoUtil.returnSuccess("获取成功", service.queryAdbannerPageByMap(param, pageNumber, pageSize));
    }

    /**
     * 新增/编辑 广告banner接口
     * @param adBanner
     * @return
     */
    @RequestMapping(value = "/csPlatform/updateAdBanner")
    @Transactional
    public Dto updateAdBanner(@RequestBody AdBanner adBanner)throws Exception {
        String msg = "新增成功";
        if (EmptyUtils.isEmpty(adBanner.getId())) {
            adBanner.setId(IdWorker.getId());
            adBanner.setCreateTime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
            adBanner.setUpdateTime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
            service.addAdbanner(adBanner);
        } else {
            adBanner.setUpdateTime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
            service.modifyAdbanner(adBanner);
        }
        return DtoUtil.returnSuccess(msg);

    }


    /**
     * 删除广告banner接口
     * @param ids
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/csPlatform/delAdBanner")
    public Dto delAdBanner(String ids)throws Exception{
        int i = 0;
        for(String id:ids.split(",")){
            i += service.deleteAdbannerById(id);
        }
        if(i>0){
            return DtoUtil.returnSuccess("操作成功", i);
        }else {
            return DtoUtil.returnFail("操作失败，请重试", "001");
        }
    }


}
